#include "aestools.h"
#include <QDebug>

AESTools::AESTools(QString *keyqs)
{

}


QByteArray AESTools::fourExchange(QByteArray &byte)
{
    int i = 0,size = byte.size();
    QByteArray newArray;

    while(i<size-3)
    {
        // 4 2 1 3
        newArray.append(byte.at(i+3));
        newArray.append(byte.at(i+1));
        newArray.append(byte.at(i));
        newArray.append(byte.at(i+2));

        i += 4;
    }

    if(i<size)
    {
        newArray.append(byte.right(size-i));
    }

    return newArray;
}

QByteArray AESTools::baseEncry(const QString &q)
{
    QByteArray data = q.toLocal8Bit().toBase64();
    return AESTools::fourExchange(data);
}

QByteArray AESTools::baseDecry(QByteArray &byte)
{
    int i = 0,size = byte.size();
    QByteArray newArray;

    while(i<size-3)
    {
        // 4 2 1 3
        newArray.append(byte.at(i+2));
        newArray.append(byte.at(i+1));
        newArray.append(byte.at(i+3));
        newArray.append(byte.at(i));

        i += 4;
    }

    if(i<size)
    {
        newArray.append(byte.right(size-i));
    }

    return QByteArray::fromBase64(newArray);
}

